<template>
  <el-dialog
    :title="form.id?'编辑部门':'新增部门'"
    :visible="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择负责人" @focus="getOptions">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click.native="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPloyeesList } from '@/api/employees'
import { getDepartmentList, addDepartment, getDepartmentById, editDepartment } from '@/api/departments.js'

export default {
  model: {
    prop: 'dialogVisible'
  },
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    const validDeptName = async(rule, value, callback) => {
      let newArr
      if (this.form.id) {
        const res = await getDepartmentList()
        newArr = res.depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id)
      } else {
        newArr = this.node.children
      }
      if (!newArr?.length) return callback()
      const flag = newArr.some(item => item.name === value)
      if (flag) return callback(new Error('部门名称已存在'))
      callback()
    }
    const validDeptCode = async(rule, value, callback) => {
      const res = await getDepartmentList()
      let newArr
      if (this.form.id) {
        newArr = res.depts.filter(item => item.code !== this.node.code)
      } else {
        newArr = res.depts
      }
      const flag = newArr.some(item => item.code === value)
      if (flag) return callback(new Error('编码已存在'))
      callback()
    }
    return {
      form: {
        name: '',
        code: '',
        introduce: '',
        manager: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' },
          { validator: validDeptName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' },
          { validator: validDeptCode, trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '请输入部门描述', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ]
      },
      options: []
    }
  },

  created() {
  },

  methods: {
    handleClose() {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('input', false)
        })
        .catch((_) => {
        })
    },
    async getOptions() {
      this.options = await getPloyeesList()
    },
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
          await editDepartment(this.form)
          this.$message.success('修改成功')
        } else {
          this.form.pid = this.node.id
          await addDepartment(this.form)
          this.$message.success('添加成功')
        }
        this.$emit('success')
        this.$emit('input', false)
      } catch (err) {
        console.log(err)
      }
    },
    async getDeptInfo(id) {
      this.form = await getDepartmentById(id)
    }
  }
}
</script>

<style scoped lang="less"></style>
